package com.easylinkin.linkappapi.lobar.util;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.easylinkin.linkappapi.tenant.constant.ProjectStatusEnum;
import com.easylinkin.linkappapi.tenant.dto.LinkappTenantDTO;

import java.util.*;

/**
 * @Author: kan yuanfeng
 * @Date: 2022/7/8 10:36
 * @Description: 劳务工具
 */
public class ProjectTenatUtils {

  // 已改为：1待建设、2建设中、3已完工、4停工中
  /**
   * 进行中合集
   */
  public static final List<Integer> projectBeingStatusList = Collections.unmodifiableList(
          Arrays.asList(ProjectStatusEnum.PENDING.getCode(), ProjectStatusEnum.IN_PROGRESS.getCode())
  );

  /**
   * 已完成合集
   */
  public static final List<Integer> projectDoneStatusList = Collections.unmodifiableList(
          Arrays.asList(ProjectStatusEnum.COMPLETED.getCode())
  );

  /**
   * 停工合集
   */
  public static final List<Integer> projectSuspendedStatusList = Collections.unmodifiableList(
          Arrays.asList(ProjectStatusEnum.SUSPENDED.getCode())
  );

  public static void calculateSurplusDay(LinkappTenantDTO project) {
    if (null == project.getProjectStatus()) {
      return;
    }
    if (projectDoneStatusList.contains(project.getProjectStatus())) {
      project.setSurplusDay(0L);
    } else if (projectSuspendedStatusList.contains(project.getProjectStatus())) {
      project.setSurplusDay(project.getSurplusDayUponSuspension());
    } else if (projectBeingStatusList.contains(project.getProjectStatus())) {
      if (null == project.getEstimateTime() || null == project.getCompletionTime()) {
        return;
      }

      long d1 = calculateD1(project);
      long d2 = calculateD2(project);
      project.setSurplusDay(d1 - d2);
    }
  }

  /**
   * 1.对于项目类型为，“在建项目”、“中标项目”、“施工准备”、“停工缓建”类型的项目：计划竣工日期-当前日期作为剩余工期；当剩余工期为负时，显示为已延期 xx天；
   * 2.对于项目类型为：已完工待结算、已完工并结算时，剩余 0天 竣工，直接显示100% 3.完成率=(当前时间-计划开工时间)/(计划竣工时间-计划开工时间)
   */
  public static void calculateProgress(LinkappTenantDTO project) {
    if (null == project.getProjectStatus()) {
      return;
    }
    if (projectDoneStatusList.contains(project.getProjectStatus())) {
      project.setProgress("1");
    } else if (projectSuspendedStatusList.contains(project.getProjectStatus())) {
      project.setProgress(project.getProgressUponSuspension());
    } else if (projectBeingStatusList.contains(project.getProjectStatus())) {
      if (null == project.getEstimateTime() || null == project.getCompletionTime()) {
        return;
      }
      long d1 = calculateD1(project);
      long d2 = calculateD2(project);

      if (d2 < 0 || d1 < 0){
        project.setProgress("0");
      } else if (d2 >= d1) {
        project.setProgress("1");
      } else {
        project.setProgress(String.valueOf(NumberUtil.div(d2, d1, 3)));
      }
    }
  }

  /**
   * 计划工期的具体天数
   * @param project
   * @return
   */
  private static long calculateD1(LinkappTenantDTO project) {
    Date startTime = DateUtil.beginOfDay(project.getEstimateTime());
    Date endTime = DateUtil.beginOfDay(project.getCompletionTime());
    // 计划工期的具体天数
    return DateUtil.between(startTime, endTime, DateUnit.DAY, false) + 1;
  }

  /**
   * 已施工天数
   * @param project
   * @return
   */
  private static long calculateD2(LinkappTenantDTO project) {
    Date startTime ;
    Date endTime = DateUtil.beginOfDay(DateUtil.date());
    if (project.getActualStartTime() != null) {
      // 实际开工时间
      startTime = DateUtil.beginOfDay(project.getActualStartTime());
    } else {
      // 预计开工时间
      startTime = DateUtil.beginOfDay(project.getEstimateTime());
    }
    return DateUtil.between(startTime, endTime, DateUnit.DAY, false) + 1;
  }


}
